package com.xm2.demo.system.employee.controller;

import com.xm2.demo.model.User;
import com.xm2.demo.system.employee.dto.EmployeeDto;
import com.xm2.demo.system.employee.dto.EmployeePage;
import com.xm2.demo.system.employee.service.EmployeeService;
import com.xm2.demo.utils.JsonResult;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Create
 * @create 2018-12-12 16:26
 * @Description：员工控制器
 */
@Controller
@RequestMapping(value = "/system/{id}/")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /*
     * description:根据不同id进入不同的界面
     * @params [pid, model]职位id
     * @return java.lang.String
     */
    @GetMapping(value = "salesman")
    public String salesman(@PathVariable("id") Integer pid, Model model){
        if (pid == null){
            return "redirect:/login/";
        }
        Pagination pagination = new Pagination();
        pagination = employeeService.findByPageable(pagination,pid);
        model.addAttribute("pagination", pagination);
        model.addAttribute("pid",pid);
        return "salesman";
    }

    @PostMapping(value = "salesman")
    @ResponseBody
    public Pagination doSalesman(@PathVariable("id") Integer pid, @RequestBody EmployeePage employeePage){
        Pagination pagination;
        if (employeePage.getPhone().equals("")){
            pagination = employeeService.findByPageable(employeePage,pid);
        } else {
            pagination = employeeService.findByEmployeePage(employeePage,pid);
        }

        return pagination;
    }
    /*
     * description:获取员工所属单位
     * @params [pid] PositionId
     * @return java.util.List<java.lang.String>
     */
    @PostMapping(value = "getPosition")
    @ResponseBody
    public List<String> getPosition(@PathVariable("id") Integer pid){
        List<String> lists = employeeService.getPosition(pid);
        return lists;
    }

    /*
     * description: 添加和更新
     * @params [employeeDto, pid]
     * @return com.xm2.demo.utils.JsonResult<com.xm2.demo.model.User>
     */
    @PostMapping(value = "addEmployee")
    @ResponseBody
    public JsonResult<User> addEmployee(@RequestBody EmployeeDto employeeDto,@PathVariable("id") Integer pid){
        if (employeeDto.getName().equals("")){
            return new JsonResult<>("101","姓名不能为空!");
        }else if (employeeDto.getPhone().equals("")){
            return new JsonResult<>("102","手机号不能为空!");
        } else if (employeeDto.getPhone().length() > 11 ){
            return new JsonResult<>("103","手机号不能超过11!");
        } else if (employeeDto.getUnit().equals("")){
            return new JsonResult<>("104","所属单位不能空!");
        } else {
            int count = employeeService.save(employeeDto,pid);
            if (count == 1){
                return new JsonResult<>();
            }else {
                return new JsonResult<>("106","手机号已存在");
            }

        }
    }
    /*
     * description:批量删除
     * @params [ids]
     * @return com.xm2.demo.utils.JsonResult
     */
    @PostMapping(value = "deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds(@RequestBody String ids){

        try {
            employeeService.deleteByIds(ids);
            return new JsonResult();
        } catch (Exception e) {
            return new JsonResult("105", "删除失败");
        }

    }

}
